﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Data.Linq;
using NetDon.Utilities;

namespace NetDon.OA.Portal.Admin
{
    public partial class GoodsShippingTypeEdit : PageBase
    {

        public Guid DeliveryOrderCodeConfigId
        {
            get { return Parameters.GetGuid("OID"); }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CancelButton = btnCancel;

                ddlCustomer.InsertEmptyItem(ListEmptyType.Select);

                if (DeliveryOrderCodeConfigId != Guid.Empty)
                {
                    var entity = new CustomerGoodsShippingTypeEntity(DeliveryOrderCodeConfigId);
                    formCurrency.DataSource = entity;
                    formCurrency.DataBind();
                    this.ddlDeliveryCodeType.SelectedValue = entity.GoodsShippedTypeId.ToString();
                    this.ddlCustomer.SelectedValue = entity.CustomerId.ToString();
                }

            }

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ObjectDataSourceCustomers.Selecting += (ds, dsArgs) =>
            {
                dsArgs.InputParameters["areaId"] = CurrentApplicationContext.CompanyId.ToString();
            };


        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var metaData = new LinqMetaData();
            var count = (from entity in metaData.CustomerGoodsShippingType
                         where entity.GoodsShippedTypeId == ddlDeliveryCodeType.SelectedValue.ToGuid() && entity.CustomerId == ddlCustomer.SelectedValue.ToGuid()
                         select entity.Id).Count();
            bool duplciateNameExists = DeliveryOrderCodeConfigId == Guid.Empty ? count > 0 : count > 1;
            if (duplciateNameExists)
            {
                Alert("此客户的承运货物配置已存在，请重新选择.");
                return;
            }
            CustomerGoodsShippingTypeEntity customerGoodsShippingTypeEntity;
            if (DeliveryOrderCodeConfigId == Guid.Empty)
            {
                customerGoodsShippingTypeEntity = new CustomerGoodsShippingTypeEntity();
                customerGoodsShippingTypeEntity.Id = Guid.NewGuid();
            }
            else
            {
                customerGoodsShippingTypeEntity = new CustomerGoodsShippingTypeEntity(DeliveryOrderCodeConfigId);
            }
            customerGoodsShippingTypeEntity.GoodsShippedTypeId = ddlDeliveryCodeType.SelectedValue.ToGuid();
            customerGoodsShippingTypeEntity.CustomerId = this.ddlCustomer.SelectedValue.ToGuid();
            customerGoodsShippingTypeEntity.AreaId = CurrentApplicationContext.CompanyId;
            if (customerGoodsShippingTypeEntity.Save())
            {
                this.CloseWindow(true);
            }

        }


    }
}